package com.ruoyi.oar.controller.pc;

import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oar.domain.dto.OarTaskImpDTO;
import com.ruoyi.oar.domain.query.OarTaskImpQuery;
import com.ruoyi.oar.service.OarTaskImpService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.Optional;

/**
 * 房间任务中间Controller
 * 
 * @author oar
 * @date 2022-02-26
 */
@Api(tags = "房间任务中间")
@RestController
@RequestMapping("/pc/oar-task-imps")
public class PcTaskImpController extends BaseController
{
    @Autowired
    private OarTaskImpService oarTaskImpService;

    /**
     * 查询房间任务中间列表
     */
    @ApiOperation("查询房间任务中间列表")
    //@PreAuthorize("@ss.hasRole('')")
    @GetMapping
    public TableDataInfo<OarTaskImpDTO> list(OarTaskImpQuery oarTaskImpQuery){
        logger.debug("查询房间任务中间列表,查询条件oarTaskImpQuery={}",JSONObject.toJSONString(oarTaskImpQuery));
        startPage();
        return oarTaskImpService.findAll(oarTaskImpQuery);
    }

    /**
     * 导出房间任务中间列表
     */
    @ApiOperation("导出房间任务中间列表")
    //@PreAuthorize("@ss.hasRole('')")
    @Log(title = "房间任务中间", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OarTaskImpQuery oarTaskImpQuery){
        logger.debug("查询房间任务中间列表,导出条件oarTaskImpQuery={}",JSONObject.toJSONString(oarTaskImpQuery));
        List<OarTaskImpDTO> list = oarTaskImpService.selectOarTaskImpList(oarTaskImpQuery);
        ExcelUtil<OarTaskImpDTO> util = new ExcelUtil<OarTaskImpDTO>(OarTaskImpDTO.class);
        return util.exportExcel(list, "房间任务中间数据");
    }

    /**
     * 获取房间任务中间详细信息
     */
    @ApiOperation("获取房间任务中间详细信息")
    //@PreAuthorize("@ss.hasRole('')")
    @GetMapping(value = "/{id}")
    public Response<OarTaskImpDTO> getInfo(@PathVariable("id") @ApiParam("房间任务中间ID") String id) {
        logger.debug("获取房间任务中间详细信息,id={}",id);
        checkId(id);
        return Response.success(oarTaskImpService.selectOarTaskImpById(id).orElseThrow(()->new ServiceException("无效的ID")));
    }

    /**
     * 新增房间任务中间
     */
    @ApiOperation("新增房间任务中间")
    //@PreAuthorize("@ss.hasRole('')")
    @Log(title = "房间任务中间", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody OarTaskImpDTO oarTaskImpDTO) {
        logger.debug("新增房间任务中间,oarTaskImpDTO={}",JSONObject.toJSONString(oarTaskImpDTO));
        return toAjax(oarTaskImpService.insertOarTaskImp(oarTaskImpDTO));
    }

    /**
     * 修改房间任务中间
     */
    @ApiOperation("修改房间任务中间")
    //@PreAuthorize("@ss.hasRole('')")
    @Log(title = "房间任务中间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OarTaskImpDTO oarTaskImpDTO) {
        logger.debug("修改房间任务中间,oarTaskImpDTO={}",JSONObject.toJSONString(oarTaskImpDTO));
        checkId(oarTaskImpDTO.getId());
        return toAjax(oarTaskImpService.updateOarTaskImp(oarTaskImpDTO));
    }

    /**
     * 删除房间任务中间
     */
    @ApiOperation("删除房间任务中间")
    //@PreAuthorize("@ss.hasRole('')")
    @Log(title = "房间任务中间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        logger.debug("删除房间任务中间,ids={}",JSONObject.toJSONString(ids));
        Arrays.asList(ids).forEach(this::checkId);
        return toAjax(oarTaskImpService.deleteOarTaskImpByIds(ids));
    }

    /**
     * 校验
     * @param id
     */
    private void checkId(String id) {
        Optional.of(id).orElseThrow(() -> new ServiceException("ID不能为空"));
        oarTaskImpService.selectOarTaskImpById(id).orElseThrow(() -> new ServiceException("无效的ID"));
    }
}
